SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS ON 
GO




ALTER     trigger [tb_commissionrawsub_au] on dbo.tb_commissionrawsub for update
as
  declare @oproductid int,
          @obid int,
          @oquantity decimal(18,4),
          @odrawqty decimal(18,4),
          @owastedqty decimal(18,4),
          @nproductid int,
          @nbid int,
          @nquantity decimal(18,4),
          @ndrawqty decimal(18,4),
          @nwastedqty decimal(18,4),
          @m_productid int
begin
  set nocount on
  if (update(drawqty) or update(wastedqty))
  begin
    if  exists( select m.bid from tb_commission m 
    left join tb_commissiondtl d on m.bid=d.bid
    left join tb_commissionraw  r  on d.bid=r.bid and d.bno=r.bno  
    left join tb_commissionrawsub s on  s.bid=d.bid and s.bno=d.bno 
    where m.bid=(select top 1 bid from inserted) and 
    (round(d.quantity - d.completedqty,8) >= 0.00000001 or round(r.drawqty - r.wastedqty,8) >= 0.00000001
    or round(s.drawqty - s.wastedqty,8) >= 0.00000001))
    begin
      update tb_commission set pause = 0 where bid = (select top 1 bid from inserted)
    end 
    else
    begin
      update tb_commission set pause = 1 where bid = (select top 1 bid from inserted)
    end
  end
end

GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO